package com.usian.controller;

import com.aliyun.ocr_api20210707.Client;
import com.aliyun.ocr_api20210707.models.RecognizeIdcardRequest;
import com.aliyun.ocr_api20210707.models.RecognizeIdcardResponse;
import com.aliyun.tea.*;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
import lombok.SneakyThrows;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping(value = "/app/user/aliyun/ocr/ocrSampleController")
public class OcrSampleController {

    /**
     * 使用AK&SK初始化账号Client
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    private static final String ACCESS_KEY_ID = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
    private static final String ACCESS_KEY_SECRET = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
    public static Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        Config config = new Config()
                .setAccessKeyId(accessKeyId)
                .setAccessKeySecret(accessKeySecret);
        config.endpoint = "ocr-api.cn-hangzhou.aliyuncs.com";
        return new Client(config);
    }

    public static void main(String[] args) {
        try {
            // 从环境变量中获取 accessKeyId 和 accessKeySecret
            String accessKeyId = ACCESS_KEY_ID;
            String accessKeySecret = ACCESS_KEY_SECRET;
            // 调用 createClient 方法
            Client client = createClient(accessKeyId, accessKeySecret);

            // 使用 client 进行后续操作
            // 例如：调用 OCR API
            // client.someOcrMethod(...);

            System.out.println("Client created successfully.");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @SneakyThrows
    @RequestMapping("/ocr")
    @ResponseBody
    public Object ocr(@RequestParam("file") MultipartFile file){
        try {
            Client client = OcrSampleController.createClient("LTAI5t7FeCfiWrnYuqZoh9R1", "iWAJFpT7ZPNDmFdwgJn0pZqKSr7mxu");
            java.io.InputStream bodyStream = file.getInputStream();
            RecognizeIdcardRequest recognizeIdcardRequest = new RecognizeIdcardRequest()
                    .setBody(bodyStream);
            RuntimeOptions runtime = new RuntimeOptions();

            // 调用 OCR API 并获取响应
            RecognizeIdcardResponse response = client.recognizeIdcardWithOptions(recognizeIdcardRequest, runtime);

            // 返回响应数据
            return response.getBody();
        } catch (TeaException error) {
            // 错误处理
            System.out.println(error.getMessage());
            System.out.println(error.getData().get("Recommend"));
            return error.getMessage();
        } catch (Exception error) {
            // 其他异常处理
            System.out.println(error.getMessage());
            return error.getMessage();
        }
    }
}
